<?php

namespace longbingcore\wxcore;

class PayModel extends \app\ApiRest
{
    protected $pay_config_data;
    public function __construct($pay_config)
    {
        $this->pay_config_data = $pay_config;
    }
    public function findOrder()
    {
        $pay_config = $this->payConfig();
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        require_once EXTEND_PATH . "alipay/aop/request/AlipayTradeQueryRequest.php";
        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $pay_config["payment"]["ali_appid"];
        $aop->rsaPrivateKey = $pay_config["payment"]["ali_privatekey"];
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $aop->apiVersion = "1.0";
        $aop->signType = "RSA2";
        $aop->postCharset = "UTF-8";
        $aop->format = "json";
        $object = new \stdClass();
        $object->out_trade_no = "20150320010101001";
        $json = json_encode($object);
        $request = new \AlipayTradeQueryRequest();
        $request->setBizContent($json);
        $result = $aop->execute($request);
        return $result;
    }
    public function adaPay($uniacid, $totalprice, $attach)
    {
        $uniacid = intval($uniacid);
        $p = new \app\adapay\info\PermissionAdapay($uniacid);
        if(!$p->pAuth()) {
            return false;
        }
        $adapay_config_model = new \app\adapay\model\Config();
        $adapay_config = $adapay_config_model->dataInfo(["uniacid" => $uniacid]);
        if($adapay_config["status"] == 1) {
            $adapay = new Adapay($uniacid);
            $adapay_code = orderCode();
            $pay_mode = $adapay_config["pay_mode"] == 1 ? "delay" : "";
            $res = $adapay->createAliPay($adapay_code, $totalprice, "alipay_wap", $pay_mode);
            if($res["status"] == "succeeded" && isset($res["expend"]["pay_info"])) {
                $pay_record = new \app\adapay\model\PayRecord();
                $dis = ["uniacid" => $uniacid, "order_code" => $attach["out_trade_no"], "type" => $attach["type"]];
                $pay_record->dataUpdate($dis, ["status" => -1]);
                $insert = ["uniacid" => $uniacid, "order_code" => $attach["out_trade_no"], "adapay_code" => $adapay_code, "adapay_id" => $res["id"], "pay_price" => $totalprice, "true_price" => $totalprice, "type" => $attach["type"], "order_id" => $attach["order_id"], "pay_mode" => $adapay_config["pay_mode"], "status" => 0];
                $pay_record->dataAdd($insert);
                $list_cache = ["order_code" => $res["id"], "type" => $attach["type"], "pay_model" => "adapay"];
                pushCache($attach["openid"] . "payresult", $list_cache, $uniacid);
                return $res;
            }
            $errlog_model = new \app\adapay\model\ErrLog();
            $errinsert = ["uniacid" => $uniacid, "text" => serialize($res), "type" => "massage_pay", "order_id" => $attach["order_id"]];
            $errlog_model->dataAdd($errinsert);
        }
        return false;
    }
    public function aliPay($order_code, $price, $subject, $type = 1, $attach = [])
    {
        if(!empty($attach) && $this->is_app == 2) {
            $res = $this->adaPay($attach["uniacid"], $price, $attach);
            if($res) {
                return $res;
            }
        }
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        require_once EXTEND_PATH . "alipay/aop/request/AlipayTradeAppPayRequest.php";
        require_once EXTEND_PATH . "alipay/aop/request/AlipayTradeWapPayRequest.php";
        $pay_config = $this->pay_config_data;
        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $pay_config["payment"]["ali_appid"];
        $aop->rsaPrivateKey = $pay_config["payment"]["ali_privatekey"];
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $aop->apiVersion = "1.0";
        $aop->signType = "RSA2";
        $aop->postCharset = "UTF-8";
        $aop->format = "json";
        $object = new \stdClass();
        $object->out_trade_no = $order_code;
        $object->total_amount = $price;
        $object->subject = $subject;
        $object->product_code = "QUICK_MSECURITY_PAY";
        $json = json_encode($object);
        $request = $pay_config["is_app"] == 1 ? new \AlipayTradeAppPayRequest() : new \AlipayTradeWapPayRequest();
        if($type == 1) {
            $request->setNotifyUrl("https://" . $_SERVER["HTTP_HOST"] . "/index.php/massage/IndexWxPay/aliNotify");
        } elseif($type == 2) {
            $request->setNotifyUrl("https://" . $_SERVER["HTTP_HOST"] . "/index.php/massage/IndexWxPay/aliNotifyBalance");
        } elseif($type == 3) {
            $request->setNotifyUrl("https://" . $_SERVER["HTTP_HOST"] . "/index.php/massage/IndexWxPay/aliNotifyUp");
        } elseif($type == 4) {
            $request->setNotifyUrl("https://" . $_SERVER["HTTP_HOST"] . "/index.php/massage/IndexWxPay/aliNotifyReseller");
        } elseif($type == 5) {
            $request->setNotifyUrl("https://" . $_SERVER["HTTP_HOST"] . "/index.php/massage/IndexWxPay/aliAgentRecharge");
        } elseif($type == 6) {
            $request->setNotifyUrl("https://" . $_SERVER["HTTP_HOST"] . "/index.php/massage/IndexWxPay/aliMemberdiscount");
        } elseif($type == 7) {
            $request->setNotifyUrl("https://" . $_SERVER["HTTP_HOST"] . "/index.php/massage/IndexWxPay/aliBalancediscount");
        }
        $request->setBizContent($json);
        $result = $pay_config["is_app"] == 1 ? $aop->sdkExecute($request) : $aop->pageExecute($request);
        return $result;
    }
    public function aliRefund($order_code, $price)
    {
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        require_once EXTEND_PATH . "alipay/aop/request/AlipayTradeRefundRequest.php";
        $pay_config = $this->pay_config_data;
        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $pay_config["payment"]["ali_appid"];
        $aop->rsaPrivateKey = $pay_config["payment"]["ali_privatekey"];
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $aop->apiVersion = "1.0";
        $aop->signType = "RSA2";
        $aop->postCharset = "UTF-8";
        $aop->format = "json";
        $object = new \stdClass();
        $object->trade_no = $order_code;
        $object->refund_amount = $price;
        $object->out_request_no = "HZ01RF001";
        $json = json_encode($object);
        $request = new \AlipayTradeRefundRequest();
        $request->setBizContent($json);
        $result = $aop->execute($request);
        $result = !empty($result) ? object_array($result) : [];
        return $result;
    }
    public function onPaymentByAlipay($alipayUserId, $nMoney, $name = "")
    {
        $pay_config = $this->pay_config_data;
        require_once EXTEND_PATH . "alipay/aop/AopClient.php";
        require_once EXTEND_PATH . "alipay/aop/request/AlipayFundTransToaccountTransferRequest.php";
        $aop = new \AopClient();
        $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
        $aop->appId = $pay_config["payment"]["ali_appid"];
        $aop->rsaPrivateKey = $pay_config["payment"]["ali_privatekey"];
        $aop->alipayrsaPublicKey = $pay_config["payment"]["ali_publickey"];
        $aop->apiVersion = "1.0";
        $aop->signType = "RSA2";
        $aop->postCharset = "UTF-8";
        $aop->format = "json";
        $request = new \AlipayFundTransToaccountTransferRequest();
        $biz_content = ["out_biz_no" => orderCode(), "payee_type" => "ALIPAY_LOGONID", "payee_account" => $alipayUserId, "amount" => $nMoney, "payee_real_name" => $name];
        $request->setBizContent(json_encode($biz_content, 256));
        $result = $aop->execute($request);
        $result = !empty($result) ? object_array($result) : [];
        return $result;
    }
}

